//
//  LaunchScreenView.swift
//  SwiftProject
//
//  Created by qindong on 2021/3/8.
//

import UIKit

class LaunchScreenView: UIView {
    var adImageView : UIImageView? = nil
    var countBtn : QMUIButton? = nil
    var launchView : UIView? = nil
    var pushBlock : (()->Void)?
    var dismissBlock : (()->Void)?
    
    var _filePath : String!
    var filePath : String! {
        set {
            _filePath = newValue
        }
        get {
            _filePath
        }
    }
    
    init(filePathString : String) {
        // 获取系统启动页
        super.init(frame: UIScreen.main.bounds)
        if #available(iOS 13.0, *) {
            let viewController : UIViewController! = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateViewController(identifier: "LaunchScreen")
            launchView = viewController.view
        } else {
            // Fallback on earlier versions
        }
        if launchView != nil {
            addSubview(launchView!)
        }
    }
    
    func showAdViewWith(time : Int) {
        adImageView = UIImageView()
        adImageView!.isUserInteractionEnabled = true
        adImageView!.clipsToBounds = true
        adImageView!.contentMode = .scaleAspectFill
        adImageView!.image = UIImage(contentsOfFile: filePath)
        adImageView!.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(pushToAd)))
        addSubview(adImageView!)

        countBtn = QMUIButton()
        countBtn!.backgroundColor = .init(red: 38/255.0, green: 38/255.0, blue: 38/255.0, alpha: 0.6)
        countBtn!.layer.cornerRadius = 5;
        countBtn!.titleLabel?.font = .systemFont(ofSize: 15)
        countBtn!.setTitleColor(.white, for: .normal)
        countBtn!.setTitle("跳过 " + String(time), for: .normal)
        countBtn!.addTarget(self, action: #selector(launchAnimationViewDissmiss), for: .touchUpInside)
        addSubview(countBtn!)

        adImageView!.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        countBtn!.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.init(width: 60, height: 30))
            make.right.equalTo(-24)
            make.top.equalTo(self).offset(PreferredValueForVisualDevice(regular: 50, compace: 30))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func pushToAd() {
        if self.pushBlock != nil {
            self.pushBlock!()
        }
    }
    
    @objc func launchAnimationViewDissmiss() {
        if self.dismissBlock != nil {
            self.dismissBlock!()
        }
    }
}
